<template>
	<view class="my-record" ref="uv-back-top">
		<uv-navbar v-if="!screen" :title="'作品记录'" @leftClick="navigateBack" :placeholder="true"></uv-navbar>
		<headers v-else></headers>
		<el-container>
			<view class="hidden-sm-and-down" style="max-width:500px">
				<my :heads="false"></my>
			</view>
			<el-main class="elMain">
				<uv-tabs :list="uvTabs" @click="click"></uv-tabs>
				<uv-list v-if="list.length>0" @scrolltolower="loadMore" class="record_content infinite-list"
					v-infinite-scroll="loadMore" :height="300">
					<uv-list-item v-for="(item,index) in list" :key="item.createdAt">
						<view class="record_list">
							<view>
								<view style="display: flex;font-size: 16px;font-weight: bold;">
									{{item.name}}
								</view>
								<view
									style="display: flex;align-content: flex-start;align-items: flex-start;flex-wrap: wrap;">
									<uv-text color="#333" size="14" :text="titlePrompt(item)"></uv-text>
									<view style="margin: 0px 5px  5px" v-for="(ite,ind) in uvImages(item)" :key="ind">
										<uv-image :src="ite" width="80px" height="80px"></uv-image>
									</view>
								</view>
							</view>
							<view style="">
								<view style="display: flex;justify-content: space-between;">
									类型:<el-tag size="mini">{{typed(item.status)}}</el-tag>
								</view>
								<view
									style="display: flex;justify-content: flex-end;align-items: center;margin-top: 3px;">
									<uv-button size="small" style="margin-right:5px" v-if="item.status==1" text="编辑"
										@click="routed('/pages/article/editor?id='+item._id)"></uv-button>
									<uv-button size="small" type="primary" text="详情"
										@click="routed('/pages/article/details?id='+item._id)"></uv-button>

								</view>

								<uv-text style="min-width:120px;" color="#000000" :size="12"
									:text="ctime(item.createdAt)"></uv-text>
							</view>
						</view>
					</uv-list-item>
					<uv-load-more v-if="list.length > 0" :status="status" :loading-text="loadingText"
						:loadmore-text="loadmoreText" :nomore-text="nomoreText" @loadmore="loadMore" />
					<el-empty v-else description="什么都没有!"></el-empty>
				</uv-list>
			</el-main>
		</el-container>
		<uv-toast ref="toast"></uv-toast>
		<uv-back-top :scroll-top="scrollTop"></uv-back-top>
	</view>
</template>

<script>
	// 引入
	import config from '@/common/config'
	import tabbars from '@/components/tabbar/tabbar';
	import my from '@/pages/my/index'
	import headers from '@/components/header/header.vue';
	import {
		getArticleName,
	} from '@/common/api.js';
	import {
		uvImages,
	} from '@/common/fun.js';
	export default {
		components: {
			// 注册组件
			tabbars,
			headers,
			my
		},
		data() {
			return {
				loading: false,
				pageSize: 15,
				page: 1,
				total: 0, //总条数
				hasMore: true, // 是否有更多数据
				status: 'nomore',

				scrollTop: 0,
				uvTabs: [{
						name: '全部',
						value: null,
					},
					{
						name: '草稿',
						value: 1,
					}, {
						name: '审核中',
						value: 2,
					}, {
						name: '正常',
						value: 3,
					}, {
						name: '新增文章',
						value: 4,
						badge: {
							isDot: true
						}
					},
				],
				list: [],
				keyword: '', //搜索框
				type: null,
				noMore: false,
				loadingText: '正在加载...',
				loadmoreText: '加载更多',
				nomoreText: '没有更多了'
			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			//重新加载
			this.page = 1;
			this.getData() //请求更多数据列表
			uni.stopPullDownRefresh(); //停止刷新
		},
		onReachBottom() {
			this.loadMore()
		},
		onPageScroll: function(e) {
			console.log("滚动距离为：" + e.scrollTop);
			if (this.hasMore) {
				this.getData(true); // 当触底时加载更多数据
			}
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
			// #ifdef APP-NVUE
			this.scrollTop = e.detail.scrollTop;
			// #endif
		},
		computed: {
			disabled() {
				return this.loading || this.noMore
			}
		},
		watch: {
			$route: {
				// immediate: true,
				handler(newRoute, oldRoute) {
					this.list = this.list.filter(item => item.sid !== this.dsgesfv);
				}
			},
		},
		mounted() {
			this.getData();
		},
		methods: {
			typed(e) {
				return this.uvTabs[e || 0].name;
				// if (e == 'painting') {
				// 	return '图片';
				// } else if (e == 'video') {
				// 	return '视频';
				// } else if (e == "music") {
				// 	return '音乐';
				// } else if (e == 'chat') {
				// 	return '对话'
				// } else {
				// 	return '未知'
				// }
			},
			titlePrompt(e) {
				if (!e) return ''; // 添加空值保护
				if (e.title) {
					return e.title;
				} else {
					if (e.content[0].userPrompt && e.content[0].videoUrl) {
						return e.content[0].userPrompt;
					}
					if (e.content[0].prompt && e.content[0].action) {
						return (e.content[0].prompt).split('--ar')[0];
					}
					if (e.content[0].prompt && e.content[0].mode) {
						return e.content[0].prompt;
					}
					if (e.content[0].revised_prompt && e.content[0].url) {
						return e.content[0].revised_prompt;
					}
					if (e.content[0]?.data && e.content[0].data[0]) {
						return e.content[0].data[0].revised_prompt
					}
					if (e.task.taskInfo.title) {
						return e.task.taskInfo.title;
					}
				}
			},
			click(item) {
				// console.log('item', item);
				if (item.value == 4) {
					this.routed('/pages/article/editor');
					return;
				}
				this.type = item.value;
				this.page = 1;
				this.getData();
			},
			ctime(time) {
				return uni.$uv.date(new Date(time).getTime(), 'yyyy/mm/dd hh:MM:ss')
			},
			loadMore() {
				if (this.status != 'nomore') {
					this.status = 'loading';
					var allTotal = this.page * this.pageSize
					if (allTotal < this.total) {
						//this.total为请求数据的总条数。只要现有条数小于总条数就执行一下代码
						this.page++;
						this.getData(true) //请求更多数据列表
					} else {
						this.status = 'nomore';
						if (this.page > 2) {
							this.$refs.toast.show({
								type: 'error',
								title: '默认主题',
								message: "没有更多数据了"
							})
						}

					}
				}

			},
			getData(ee = false) {
				var that = this;
				getArticleName('list', {
					custom: {
						auth: true,
						// toast: false,
						catch: true,
					},
					params: {
						pageSize: this.pageSize,
						page: this.page,
						status: this.type,
					}
				}).then(e => {
					if (ee) {
						this.list = [...this.list, ...(e.data || [])]; // 安全连接数组
					} else {
						this.list = e.data || [];
					}
					this.status = 'loadmore';
					this.page = e.page;
					this.pageSize = e.pageSize;
					this.total = e.total;
				}).catch(e => {
					console.log(e);
				})
			},
			routed(url) {
				uni.$uv.route({
					url: url,
					// intercept(params, next) {
					// 	uni.showLoading({
					// 		title: '等待中...'
					// 	})
					// 	// 模拟处理等待
					// 	setTimeout(() => {
					// 		uni.hideLoading();
					// 		next(true);
					// 	}, 1000)
					// }
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.my-record {
		padding: 0px;
		width: 100%;

		.elMain {
			padding: 0px;
			max-width: 900px;
			border: 1px solid #eee;
			max-height: calc(100vh - 55px);
			margin: 5px;
			border-radius: 5px;

			::v-deep .record_content {
				overflow: auto;
				padding: 0px;
				max-height: calc(100% - 55px);

				.record_list {
					display: flex;
					justify-content: space-between;
					align-items: center;
					border: 2px solid #eeeeee;
					margin: 10px 5px;
					padding: 10px;
					background-color: rgba(255, 255, 255, 0.1);
					border-radius: 10px;
				}
			}

			::v-deep .uv-list-item {
				background-color: #000;
			}
		}

		.elMain_os {
			max-height: calc(100vh - 150px);
		}

	}
</style>